package com.dhc.redpacket.controller;

import com.dhc.redpacket.pojo.RedPacket;
import com.dhc.redpacket.service.RedPacketService;
import com.dhc.redpacket.service.UserRedPacketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/userRedPacket")
public class UserRedPacketController {

  @Autowired
  private UserRedPacketService userRedPacketService;

  @Autowired
  private RedPacketService redPacketService;

  @RequestMapping(value = "/grapRedPacket", method = RequestMethod.POST)
  @ResponseBody
  public Boolean grapRedPacket(@RequestBody Map<String, Long> map) throws Exception {
    int i = userRedPacketService.grapRedPacket(map.get("redPacketId"), map.get("userId"));
    if (i == 1) {
      return true;
    }
    return false;
  }

  @RequestMapping(value = "/grapRedPacketForVersion", method = RequestMethod.POST)
  @ResponseBody
  public Boolean grapRedPacketForVersion(@RequestBody Map<String, Long> map) throws Exception {
    int i = userRedPacketService.grapRedPacketForVersion(map.get("redPacketId"), map.get("userId"));
    if (i == 1) {
      return true;
    }
    return false;
  }

  @RequestMapping("/getInfo")
  @ResponseBody
  public RedPacket getInfo(@RequestParam("id") Long id) throws Exception {
    return redPacketService.getRedPacket(id);
  }
}
